package com.sdkj.dspservice.modules.doc.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdkj.dspservice.common.base.controller.BaseController;
import com.sdkj.dspservice.common.base.entity.BaseResult;
import com.sdkj.dspservice.common.config.Global;
import com.sdkj.dspservice.common.react.AntdProResult;
import com.sdkj.dspservice.common.utils.StringUtils;
import com.sdkj.dspservice.modules.doc.entity.Doc;
import com.sdkj.dspservice.modules.doc.service.DocService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * 文件Controller
 *
 * @author tm
 * @version 2020/10/19 20:32
 */
@RestController
@RequestMapping(value = "${project-config.apiUrl}/${project-config.version}/doc/doc")
public class DocController extends BaseController {

    @Autowired
    private DocService docService;

    /**
     * 获取文件
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "get")
    public AntdProResult get(String id) {
        AntdProResult result = new AntdProResult();
        if (StringUtils.isNotBlank(id)) {
            Doc doc = docService.getById(id);
            result.success(doc);
            // 增加浏览次数
            doc.addBrowseNumber();
            docService.saveOrUpdate(doc);
        } else {
            result.error(AntdProResult.SYSTEM_ERROR, "未传入id");
        }
        return result;
    }

    /**
     * 获取列表
     *
     * @param doc
     * @return
     */
    @RequestMapping(value = "list")
    public AntdProResult list(Doc doc) {
        AntdProResult result = new AntdProResult();
        // name查询实际，name和description其中一个满足即可
        QueryWrapper<Doc> queryWrapper = new QueryWrapper<>();
        doc.makeQueryWrapper(queryWrapper);
        List<Doc> docList = docService.listLink(queryWrapper);
        result.success(docList);
        return result;
    }

    /**
     * 获取热门列表
     *
     * @return
     */
    @RequestMapping(value = "hotList")
    public AntdProResult hotList() {
        AntdProResult result = new AntdProResult();
        List<Doc> docList = docService.findHotList();
        result.success(docList);
        return result;
    }

    /**
     * 保存
     *
     * @param doc
     * @return
     */
    @RequestMapping(value = "save")
    public AntdProResult save(@Valid @RequestBody Doc doc) {
        AntdProResult result = new AntdProResult();
        docService.saveOrUpdate(doc);
        return result;
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @RequiresPermissions("doc:doc:edit")
    @RequestMapping(value = "remove")
    public BaseResult delete(String id) {
        BaseResult result = new BaseResult();
        docService.removeById(id);
        return result;
    }

    /**
     * 增加浏览次数
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "addBrowseNumber")
    public AntdProResult addBrowseNumber(String id) {
        AntdProResult result = new AntdProResult();
        Doc doc = docService.getById(id);
        doc.addBrowseNumber();
        docService.saveOrUpdate(doc);
        return result;
    }

    /**
     * 增加下载次数
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "addDownloadNumber")
    public AntdProResult addDownloadNumber(String id) {
        AntdProResult result = new AntdProResult();
        Doc doc = docService.getById(id);
        doc.addDownloadNumber();
        docService.saveOrUpdate(doc);
        result.success(doc, "下载成功！");
        return result;
    }

    /**
     * 增加收藏次数
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "addGoodNumber")
    public AntdProResult addGoodNumber(String id) {
        AntdProResult result = new AntdProResult();
        Doc doc = docService.getById(id);
        doc.addGoodNumber();
        docService.saveOrUpdate(doc);
        result.success(null, "点赞成功！");
        return result;
    }

    /**
     * 置顶
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "setTop")
    public AntdProResult setTop(String id) {
        AntdProResult result = new AntdProResult();
        Doc doc = docService.getById(id);
        doc.setIsTop(Global.YES);
        docService.saveOrUpdate(doc);
        result.success(null, "置顶成功！");
        return result;
    }

}
